//
//  FJMessageViewController.swift
//  FruitJuice
//
//  Created by Rae on 2023/12/15.
//

import UIKit

class FJMessageViewController: FJBaseViewController {

    private let backView            = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_bg").end()
    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.black).font(23.font_wide).text("Message").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 85
        tableView.register(FJMessageViewCell.self)

        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        
        view.subviews{
            backView.subviews{
                titleLabel
                tableView
            }
            backButton
        }
        
        backView.fillContainer()
        backButton.top(.statusHeight + 7).leading(15).size(30)
        titleLabel.leading(15)
        titleLabel.Top == backButton.Bottom + 15
        tableView.leading(0).trailing(0).bottom(.safe_bottom)
        tableView.Top == titleLabel.Bottom
        
        backView.isUserInteractionEnabled = true
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}

extension FJMessageViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        itemInfos.count
        return 15
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(FJMessageViewCell.self, indexPath: indexPath)
//        cell.itemType = itemInfos[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.pushViewController(FJChatViewController(), animated: true)
//        tableView.deselectRow(at: indexPath, animated: true)
//        switch itemInfos[indexPath.row]{
//        case .delete:
//            let deleteV = LearningDeleteAccountView()
//            saAnimate.animation(show: deleteV)
//            break
//        case .erms:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Terms of use"
//            webVC.webUrl = URLTool.technicalSupportUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
//            break
//        case .privacy:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Privacy Policy"
//            webVC.webUrl = URLTool.privacyPolicyUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
//            break
//        case .cache:
//            let clearView = LearningClearCacheView()
//            clearView.didCleanCompletion = { [weak self] in
//                self?.tableView.reloadData()
//            }
//            saAnimate.animation(show: clearView)
//            break
//        default:
//            return
//        }
//
    }
}


class FJMessageViewCell: UITableViewCell {
    
    private let backView            = c_make_ex(UIView.self)
        .bgColor("#EFEFEF").corner(35).end()
    private let iconImageView       = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).bgColor(.lightGray).corner(25).end()
    private let nameLabel           = c_make_ex(UILabel.self)
        .color(.black).text("Glosha").font(16.font_bold).end()
    private let contentLabel        = c_make_ex(UILabel.self)
        .color(.black).text("Camping together on weekends?").font(14.font_regular).end()
    private var unreadLabel         = LabelWithPadding()
    private let timeLabel           = c_make_ex(UILabel.self)
        .color(.black).text("06:20").font(12.font_regular)
        .alignment(.right).end()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            backView.subviews{
                iconImageView
                nameLabel
                contentLabel
                unreadLabel
                timeLabel
            }
        }
        backView.leading(15).trailing(15).top(15).bottom(0)
        iconImageView.leading(10).size(50).centerVertically()
        nameLabel.leading(70).trailing(80).top(15)
        contentLabel.trailing(80).leading(70)
        contentLabel.Top == nameLabel.Bottom + 3
        unreadLabel.trailing(18)
        unreadLabel.CenterY == nameLabel.CenterY
        timeLabel.trailing(10)
        timeLabel.CenterY == contentLabel.CenterY
        
        
        unreadLabel.style { v in
            v.font = 12.font_regular
            v.textAlignment = .center
            v.layer.cornerRadius = 9.0
            v.layer.masksToBounds = true
            v.text = "9"
            v.textColor = .white
            v.padding = UIEdgeInsets(top: 2, left: 8, bottom: 2, right: 8)
            v.backgroundColor = "#FF4500".toColor
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
